package com.partner.share.work.websocket;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;

/**
 * @author pengyangyan
 */
@Slf4j
public class WebSocketUtil {

    private WebSocketUtil(){
        //私有构造，禁止new
    }

    /**
     * 广播
     * @param message 消息内容
     * @param sessionSet session集合
     */
    public static void broadcast(String message,Set<Session> sessionSet){
        if (sessionSet.size()==0){
            return;
        }
        sessionSet.forEach(session -> {
            try {
                session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                log.error("消息推送失败:{}",e.getMessage());
                e.printStackTrace();
            }
        });
    }

    /**
     * 单点推送
     * @param message 消息内容
     * @param session session
     */
    public static void sendOne(String message,Session session){
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            log.error("消息推送失败:{}",e.getMessage());
            e.printStackTrace();
        }
    }


}
